//
//  UUNotificationVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 14/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUNotificationVC.h"
#import "UUNotificationManager.h"
#import "UUNotificationStateListVC.h"
#import <WZLBadge/WZLBadgeImport.h>
#import "AppDelegate.h"

@interface UUNotificationVC () <UUNotificationManagerObserver>

@property (nonatomic, weak) DKPagingMenuItem *notificationItem;
@property (nonatomic, weak) DKPagingMenuItem *productItem;
@property (nonatomic, weak) DKPagingMenuItem *newsItem;

@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) UUNotificationUnreadCountModel *unreadCount;

@end

@implementation UUNotificationVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"消息";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *itemMenu = [[NSMutableArray alloc] initWithCapacity:0];
    DKPagingMenuItem *notificationItem = [[UUPagingMenuItem alloc] init];
    notificationItem.title = @"提醒通知";
    notificationItem.menuViewController = [UUNotificationStateListVC listWithType:UUNotificationTypeMessage];
    [itemMenu addObject:notificationItem];
    
    DKPagingMenuItem *productItem = [[UUPagingMenuItem alloc] init];
    productItem.title = @"产品动态";
    productItem.menuViewController = [UUNotificationStateListVC listWithType:UUNotificationTypeProduct];
    [itemMenu addObject:productItem];
    
    DKPagingMenuItem *newsItem = [[UUPagingMenuItem alloc] init];
    newsItem.title = @"资讯活动";
    newsItem.menuViewController = [UUNotificationStateListVC listWithType:UUNotificationTypeNews];
    [itemMenu addObject:newsItem];
    
    UUPagingMenuController *pagingMenuController = [[UUPagingMenuController alloc] initWithItems:itemMenu];
    [self addChildViewController:pagingMenuController];
    [self.view addSubview:pagingMenuController.view];
    [pagingMenuController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        make.left.right.mas_equalTo(0);
    }];
    
    WEAK_SELF();
    [pagingMenuController setDidSelectIndexBlock:^(NSInteger index) {
        [weakSelf clearBadgeForCurrentIndex];
        weakSelf.currentIndex = index;
    }];
    
    self.notificationItem = notificationItem;
    self.productItem = productItem;
    self.newsItem = newsItem;
    
    [GetNotificationManager() addObserver:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self updateBadges];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    if (GetAppDelegate().tabBarController.selectedIndex != UUTabBarTypeNotification) {
        [self clearBadgeForCurrentIndex];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clearBadgeForCurrentIndex {
    switch (self.currentIndex) {
        case 0:
            [GetNotificationManager() clearUnreadCountForMessage];
            break;
        case 1:
            [GetNotificationManager() clearUnreadCountForProduct];
            break;
        case 2:
            [GetNotificationManager() clearUnreadCountForNews];
            break;
        default:
            DKAssert(0);
            break;
    }
}

- (void)updateBadges {
    UUNotificationUnreadCountModel *unreadCount = [GetNotificationManager() fetchNotificationUnreadCount];
    [self.notificationItem.view showBadgeWithStyle:WBadgeStyleNumber
                                             value:unreadCount.message
                                     animationType:WBadgeAnimTypeNone];
    if (unreadCount.message > 0 && unreadCount.message != self.unreadCount.message) {
        [(UUNotificationStateListVC *)self.notificationItem.menuViewController showNewNotificationBadge];
    }
    
    [self.productItem.view showBadgeWithStyle:WBadgeStyleNumber
                                        value:unreadCount.product
                                animationType:WBadgeAnimTypeNone];
    if (unreadCount.product > 0 && unreadCount.product != self.unreadCount.product) {
        [(UUNotificationStateListVC *)self.productItem.menuViewController showNewNotificationBadge];
    }
    
    [self.newsItem.view showBadgeWithStyle:WBadgeStyleNumber
                                     value:unreadCount.news
                             animationType:WBadgeAnimTypeNone];
    if (unreadCount.news > 0 && unreadCount.news != self.unreadCount.news) {
        [(UUNotificationStateListVC *)self.newsItem.menuViewController showNewNotificationBadge];
    }
    
    self.unreadCount = unreadCount;
}

#pragma mark - UUNotificationManagerObserver methods

- (void)notificationManagerDidUpdateUnreadCount {
    [self updateBadges];
}

@end
